// pages/booking/booking.js
const app = getApp();
var common = require("../../resources/config/config.js");
var utils = require("../../utils/util.js");
var messages = require("../../utils/messages.js");

Page({
  data: {
    items: [
      { name: 'ZH', value: '中文', checked: 'true' },
      { name: 'EN', value: 'English' },
    ],
    labels: ['日期', '运动'],
    sports_lang: [],
    sports: [],
    courts: [],
    times: [],
    prices: [],
    language: 'ZH',
    disabled: [false, true],
    date: '',
    index: null,
    confirm:'确认',
  },
 
  onLoad: function (options){
    var self = this;
    loadSports(function(result){
      self.setData({
        sports_lang: result,
        sports: result.zh
      })
      app.globalData.sports_with_lang=result;
    })
    var today = new Date();
    this.setData({
      date: utils.formatTime(today)
    })
  },

  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value);
    if (e.detail.value == 'EN') {
      this.setData({
        language: 'EN',
        labels: ['Date', 'Sport'],
        sports: this.data.sports_lang.en,
        confirm: 'Confirm'
      })
      app.globalData.messages = messages.messages.en;
    }
    else if (e.detail.value == 'ZH') {
      this.setData({
        language: 'ZH',
        labels: ['日期', '运动'],
        sports: this.data.sports_lang.zh,
        confirm: '确认'
      })
      app.globalData.messages = messages.messages.zh;
    }
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
    if (this.data.date != '' & this.data.index != null) {
      this.setData({
        disabled: [false, false]
      })
    }
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
    if (this.data.date != '' & this.data.index != null) {
      this.setData({
        disabled: [false, false]
      })
    }
  },
  bindNavigate: function (e) {
    var x = '';
    var sportId = this.data.sports_lang.sportIds[this.data.index];
    x = '../court/court' + '?language=' + this.data.language + '&date=' + this.data.date + '&index=' + this.data.index+"&sportId="+sportId;
    console.log(x);
    wx.navigateTo({
      url: x,
    })
  }
})

function loadSports(loadsport_callback) {
  console.log("loading sports from backoffice");
  var url = common.getApiLink("getSports");
  console.log(url);
  utils.req_get(url, function (result) {
    var sports = {
      sportIds: [],
      zh: [],
      en: []
    }
    for (var i = 0; i < result.sports.length; i++) {
      sports.sportIds.push(result.sports[i].sportId);
      sports.zh.push(result.sports[i].zhName);
      sports.en.push(result.sports[i].enName);
    }
    loadsport_callback(sports);
  });
}